package com.yizhu.zczy.system.controller;

import com.yizhu.zczy.common.utils.R;
import com.yizhu.zczy.system.constant.SystemConstant;
import com.yizhu.zczy.system.entity.UserEntity;
import com.yizhu.zczy.system.interceptor.MyHandlerInterceptor;
import com.yizhu.zczy.system.service.UserService;
import com.yizhu.zczy.system.vo.ChangePasswordVo;
import com.yizhu.zczy.system.vo.UserNameAndPassWordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@RequestMapping("/user")
@Api("用户登录控制器")
public class UserLoginController {
    @Autowired
    private UserService userService;

    @PostMapping("用户登录名和密码")
    public R login(@Valid UserNameAndPassWordVo userNameAndPassWordVo, HttpSession session){
        try {
            userService.login(userNameAndPassWordVo,session);
        }catch (Exception e){
            return R.error(e.getMessage());
        }
        return R.ok();
    }

    @GetMapping("/logout")
    @ApiOperation("退出登录")
    public R logout(HttpSession session){
      session.removeAttribute(SystemConstant.USER_SESSION_SAVE);
      return R.ok();
    }

    @PostMapping("/changePassword")
    @ApiOperation("修改密码")
    public R changePassword(ChangePasswordVo passwordVo){
        try {
            userService.changePassword(passwordVo);
            return R.ok();
        }catch (Exception e){
          return R.error(e.getMessage());
        }
    }
}
